package com.xasz.text2vec.utils;


import java.util.List;

public class Cal_sta {
    /**
     * 求和
     */
    double sum(List<Double> data) {
        double sum = 0;
        for (Double datum : data) {
            sum = sum + datum;
        }
        return sum;
    }

    /**
     * 平均值
     */
    double mean(List<Double> data) {
        double mean = 0;
        mean = sum(data) / data.size();
        return mean;
    }

    /**]
     * 总体方差
     */
    public double POP_Variance(List<Double> data) {
        double variance = 0;
        for (int i = 0; i < data.size(); i++) {
            variance = variance + (Math.pow((data.get(i) - mean(data)), 2));
        }
        variance = variance / data.size();
        return variance;
    }

    /**
     * 总体标准差
     */
    public double POP_STD_dev(List<Double> data) {
        double std_dev;
        std_dev = Math.sqrt(POP_Variance(data));
        return std_dev;
    }

    /**
     * 样本方差
     */
    public double Sample_Variance(List<Double> data) {
        double variance = 0;
        for (int i = 0; i < data.size(); i++) {
            variance = variance + (Math.pow((data.get(i) - mean(data)), 2));
        }
        variance = variance / (data.size()-1);
        return variance;
    }

    /**
     * 样本标准差
     */
    public double Sample_STD_dev(List<Double> data) {
        double stdDev;
        stdDev = Math.sqrt(Sample_Variance(data));
        return stdDev;
    }

    /**
     * 协方差
     */
    public double covariance(List<List<Double>> data) {
        //计算协方差
        double sum=0.0;
        double Ex=mean(data.get(0));
        double Ey=mean(data.get(1));
        for (int i = 0; i < data.get(0).size(); i++) {
            Double X = data.get(0).get(i);
            Double Y = data.get(1).get(i);
            sum+=(X-Ex)*(Y-Ey);
        }
        return sum/data.get(0).size();
    }
    /**
     * 相关系数 p=cov/(aX*aY)
     */
    public double correlation(List<List<Double>> data) {
        double cov = covariance(data);
        double aX = POP_STD_dev(data.get(0));
        double aY = POP_STD_dev(data.get(1));
        return cov/(aX*aY);
    }

}
